 export default {
 	namespaced: true, //其成为带命名空间的模块
 	state: {
 		selectedHouse: '',
		getcartdata:uni.getStorageSync('cartdata') || [],
 	},
 	mutations: {
		GETCARTDATA(state,goods){
			state.getcartdata = goods
			this.commit('qi_cart/saveToStorageGETCARTDATA')
		},
		// 提交选中删除
		deletecart(state,goods){
		state.getcartdata = 	state.getcartdata.filter(item=> item.id != goods)
 			this.commit('qi_cart/saveToStorageGETCARTDATA')
		},
		// 更新购物车中商品的勾选状态
		updateGoodsStateCARTDATA(state, goods) {
			const findResult = state.getcartdata.find(x => x.id === goods.id)
			if (findResult) {
				findResult.goods.is_checked = goods.is_checked
				this.commit('qi_cart/saveToStorageGETCARTDATA')
			}
		},
	// 更新商品的数量
	updateGoodsCountCARTDATA(state, goods) {
		const findResult = state.getcartdata.find(x => x.id === goods.id)
		findResult.num = goods.num
		this.commit('qi_cart/saveToStorageGETCARTDATA')
	},

		// 删除商品
		DELETEGOODSCARTDATA(state, goods) {
			state.getcartdata = state.getcartdata.filter(x => x.id !== goods)
			this.commit('qi_cart/saveToStorageGETCARTDATA')
		},

	// 更新购物车中所有商品的勾选状态
	updateAllGoodsStateCARTDATA(state, newState) {
	  state.getcartdata.forEach(x => x.is_checked = newState)
	  this.commit('qi_cart/saveToStorageGETCARTDATA')
	},

		saveToStorageGETCARTDATA(state){
		uni.setStorageSync('cartdata', state.getcartdata)
		}
 	},
	getters:{
		total(state) {
			return state.getcartdata?.reduce((total, item) =>
			 total += item.num, 0)
		},
		// 已勾选商品的总价格
		 checkedGoodsAmount(state) {
		   return state.getcartdata.filter(x => x.is_checked).reduce((total, item) => 
			 total += item.num * item.goods.price, 0).toFixed(2)
		 },
		 // 购物车中已勾选商品的总数量
		 checkedCount(state) {
			return state.getcartdata.filter(x => x.is_checked).reduce(
			 (total, item) => total += item.num, 0)
			
		  //  return state.getcartdata.filter(x => x.is_checked).reduce(
			 // (total, item) => total += item.is_checked, 0)
		 },
	},
 	actions: {
 		addDataListCart(context, goods) {
 			return new Promise(resolve => {
 				const findResult = context.state.cart.find(x => x.goods_id === goods.goods_id
)
				console.log(context.state.cart);
 				if (!findResult) {
 					context.commit('increment', goods)
 				} else {
 					findResult.goods_count++
 				}
 				resolve('加入成功')
 			});
 		},

 	},


 }
